package com.autonavi.yunda.yunji.controller;

import com.autonavi.yunda.yunji.configuration.exception.ResourceNotFoundException;
import com.autonavi.yunda.yunji.core.engine.Engine;
import com.autonavi.yunda.yunji.core.engine.script.InterfaceScriptClass;
import com.autonavi.yunda.yunji.core.engine.script.ScriptClassManager;
import com.autonavi.yunda.yunji.core.vo.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cizhong.wcs
 * @date 2022/3/7 8:50 下午
 */
@RestController
public class ApiController {
    @Resource
    private Engine engine;
    @Resource
    private ScriptClassManager scriptClassManager;

    @RequestMapping("/**")
    public Object rest(HttpServletRequest request, HttpServletResponse response) {
        InterfaceScriptClass scriptClass = scriptClassManager.fetchScriptClassByUriFromCache(request.getRequestURI());
        if (scriptClass == null) {
            throw new ResourceNotFoundException();
        }
        return engine.exeApi(scriptClass, new HttpRequest(request, scriptClass.engineData.fetchStartNode().contentType), response);
    }

}
